package com.ruoyi.bom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.ruoyi.bom.config.RequestDataHelper;
import com.ruoyi.bom.domain.TqlMaterialTransfer;
import com.ruoyi.bom.dymapper.TqlKingdeeTransferMapper;
import com.ruoyi.bom.service.TqlKingdeeTransferService;
import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.utils.uuid.Seq;
import com.ruoyi.common.datasource.annotation.Slave;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: Jzh
 * @Date: 2024-01-24  19:08
 */
@Service
public class TqlKingdeeTransferServiceImpl implements TqlKingdeeTransferService {

    @Autowired
    private TqlKingdeeTransferMapper tqlKingdeeTransferMapper;

    @Override
    @Slave
    public List<TqlMaterialTransfer> selectTransferList(Map<String, String> params) {
        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH) + 1;

        String date = params.get("date");
        if (StringUtils.isNotBlank(date)) {
            try {
                // 创建SimpleDateFormat对象，用于解析日期字符串
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // 获取日期对象中的日部分
                month = sdf.parse(date).getMonth();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        QueryWrapper<TqlMaterialTransfer> wrapper = new QueryWrapper<>();
        wrapper.apply(StringUtils.isNotBlank(date),"date_format(createTime, '%Y-%m-%d') ='" + date + "'");
        wrapper.orderByDesc("createTime");

        int finalMonth = month;
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("index", finalMonth);
        }});
        List<TqlMaterialTransfer> list = tqlKingdeeTransferMapper.selectList(wrapper);
        return list;
    }

    @Override
    @Slave
    public void add(List<TqlMaterialTransfer> dtoList) {

        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH) + 1;
        // 传递动态表名所需参数
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("index", month);
        }});

        String createdBy = SecurityUtils.getUsername();
        String fbillno = Seq.getId();

        List<TqlMaterialTransfer> outInList = new ArrayList<>();
        TqlMaterialTransfer out;
        for (TqlMaterialTransfer dto : dtoList) {
            dto.setFlag("1");
            dto.setType(2);
            dto.setFDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
            dto.setCreateBy(createdBy);
            dto.setCreateTime(new Date());
            dto.setFbillno(fbillno);
            Random random = new Random();
            long randomNumber = (long)(random.nextInt(9000000) + 1000000);
            dto.setFDID(randomNumber);
            //调入处理
            tqlKingdeeTransferMapper.insert(dto);
            dto.setType(3);
            outInList.add(dto);
            //调出处理
            out = dto;
            out.setId(null);
            out.setFlag("-1");
            String FStockOutOrgfnumber, FStockOutOrgIDfname, FStockInOrgIDfnumber, FStockInOrgIDfname,
                   FOUTDEPTIDfnumber, FOUTDEPTIDfname, FINDEPTIDfnumber, FINDEPTIDfname;
            FStockOutOrgfnumber = dto.getFStockOutOrgfnumber();
            FStockOutOrgIDfname = dto.getFStockOutOrgIDfname();
            FStockInOrgIDfnumber = dto.getFStockInOrgIDfnumber();
            FStockInOrgIDfname = dto.getFStockInOrgIDfname();

            FOUTDEPTIDfnumber = dto.getFOUTDEPTIDfnumber();
            FOUTDEPTIDfname = dto.getFOUTDEPTIDfname();
            FINDEPTIDfnumber = dto.getFINDEPTIDfnumber();
            FINDEPTIDfname = dto.getFINDEPTIDfname();

            out.setFStockOutOrgfnumber(FStockInOrgIDfnumber);
            out.setFStockOutOrgIDfname(FStockInOrgIDfname);

            out.setFStockInOrgIDfnumber(FStockOutOrgfnumber);
            out.setFStockInOrgIDfname(FStockOutOrgIDfname);

            out.setFOUTDEPTIDfnumber(FINDEPTIDfnumber);
            out.setFOUTDEPTIDfname(FINDEPTIDfname);

            out.setFINDEPTIDfnumber(FOUTDEPTIDfnumber);
            out.setFINDEPTIDfname(FOUTDEPTIDfname);
            long randomNumber1 = (long)(random.nextInt(9000000) + 1000000);
            out.setFDID(randomNumber1);
            tqlKingdeeTransferMapper.insert(out);
            dto.setType(4);
            outInList.add(out);
        }
        String tabname = CommonUtil.TABLENAME_TQL_KINGDEE_TRANSFER+ month;
        tqlKingdeeTransferMapper.batchAddTransferoutIn(outInList, tabname);
    }

    @Override
    public List<TqlMaterialTransfer> getByFbillno(String fbillno, Integer month, String FMaterialId) {
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("index", month);
        }});
        QueryWrapper<TqlMaterialTransfer> wrapper = new QueryWrapper<>();
        wrapper.eq("fbillno", fbillno);
        wrapper.eq("flag", 1);
        wrapper.eq(StringUtils.isNotBlank(FMaterialId),"FMaterialId", FMaterialId);
        return tqlKingdeeTransferMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public void update(List<TqlMaterialTransfer> dtoList) {
        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH) + 1;
        // 传递动态表名所需参数
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("index", month);
        }});

        String createdBy = SecurityUtils.getUsername();
        for (TqlMaterialTransfer dto : dtoList) {
            dto.setType(2);
            dto.setCreateBy(createdBy);
            dto.setCreateTime(new Date());
            if (StringUtils.isBlank(dto.getFbillno())){
                throw new ServiceException("Fbillno不能为空！");
            }
            String flag = dto.getFlag();
            if (flag.equals("-1")) {
                tqlKingdeeTransferMapper.updateById(dto);
                QueryWrapper<TqlMaterialTransfer> wrapper = new QueryWrapper<>();
                wrapper.eq("fbillno", dto.getFbillno());
                wrapper.eq("FMaterialId", dto.getFMaterialId());
                wrapper.eq("flag", 1);
                wrapper.last("limit 1");
                TqlMaterialTransfer newdto = tqlKingdeeTransferMapper.selectOne(wrapper);
                newdto.setFQty(dto.getFQty());
                newdto.setFNegotiateAmount(dto.getFNegotiateAmount());
                tqlKingdeeTransferMapper.updateById(newdto);
            }else {
                //调入处理
                tqlKingdeeTransferMapper.updateById(dto);
                //调出处理
                QueryWrapper<TqlMaterialTransfer> wrapper = new QueryWrapper<>();
                wrapper.eq("fbillno", dto.getFbillno());
                wrapper.eq("FMaterialId", dto.getFMaterialId());
                wrapper.eq("flag", -1);
                wrapper.last("limit 1");
                TqlMaterialTransfer newdto = tqlKingdeeTransferMapper.selectOne(wrapper);
                newdto.setFQty(dto.getFQty());
                newdto.setFNegotiateAmount(dto.getFNegotiateAmount());
                tqlKingdeeTransferMapper.updateById(newdto);
            }
        }
    }

}
